using CxRomos.Core.DataAccess;
using CxRomos.Core.Services;
using CxRomos.Core.Services.Implementation;
using CxRomos.Web.Account.Interface;
using StructureMap;

namespace CxRomos.Web.Account.Presenter {
    public class VerifyEmailPresenter {
        private IWebContext _webContext;
        private IAccountRepository _accountRepository;
        public void Init(IVerifyEmail view) {
            _webContext = ObjectFactory.GetInstance<IWebContext>();
            _accountRepository = ObjectFactory.GetInstance<IAccountRepository>();

            var username = Cryptography.Decrypt(_webContext.UsernameToVerify, "verify");

            var account = _accountRepository.GetAccountByUsername(username);

            if (account != null) {
                account.EmailVerified = true;
                _accountRepository.SaveAccount(account);
                view.ShowMessage("Your email address has been successfully verified!");
            } else {
                view.ShowMessage("There appears to be something wrong with your verification link!  Please try again. " + 
                    "If you are having issues by clicking on the link, please try copying the URL from your email and pasting it into your browser window.");
            }
        }

    }
}